<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<? 
$uploaddir = "./files/"; 
$url = 'http://'.$_SERVER['SERVER_NAME'].dirname($_SERVER['PHP_SELF']);
$action = $_POST['action'];
$id =$_POST['id'];
$file_onserver = $uploaddir.$_POST['fn']; 
$temp_file = $_FILES['userfile']['tmp_name'];
echo '<pre>';
?>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Refresh" content="10;url=<?php  echo $url;?>;">
<title>处理中</title>
</head>

<body>

<?php

echo "处理中。。。。";
require_once("dbase.php");
$db=new dbase();

if($action=="delete")//如果是删除文件
{
	$arr = $db->getdata("`id`=".$id);
	$update['status'] = "disable";	
	foreach($arr as $k=>$v)
	{
		$file =$uploaddir.$v['url'];
	if (is_file($file)) {
		unlink($file);
		$db->update($id,$update,"tb_files");
		 echo " 文件删除成功!";
		 }
	else {
		echo " 文件不存在".$file;
		$db->delete($id,"tb_files");
	}
	
	}
}
else if($action=="add") //如果是增加文件
{
	$tmpname = date("YmdHis");	
	$uploadfile = $uploaddir.$_FILES['userfile']['name'];
	$getExt = pathinfo($uploadfile);	// extension
	$newfile = $tmpname.".".$getExt['extension']; 
	$uploadfile = $uploaddir.$newfile;// url 
	$file_name = $getExt['basename']; // file name
	
	// file_name --- url---extension
	if(move_uploaded_file($temp_file, $uploadfile))
	{
		echo "增加文件成功!...";//使用随机命名保存文件至指定路径
		$array['id'] = 'NULL';
		$array['file_name'] = $file_name;
		$array['extension'] = $getExt['extension'];
		$array['url'] = $newfile;
		$array['clicks'] = '0';
		$array['date_time'] = date("Y-m-d H:i:s");//'2011-07-19 15:46:06';
		$array['status'] = 'active';
		$array['version'] = '1';
		$array['owner'] = 'unknown';		
		print_r ($array);
		$db->insert($array,"tb_files");
		echo "数据库更新成功!";
	}
	else 
	{
		echo "增加文件失败!<br /> 单击<a href ='".$url."'>这里</a>返回首页。";
		die();
		
	}

	
}
else if($action=="update")
{//如果是更新文件

	//print_r($path_parts);
	//STEPS1 :从数据库找到该ID对应的文件名和扩展名;
	// 判断源文件 和 新文件之间 的差异
	$arr = $db->getdata("`id`=".$id);
	foreach($arr as $k=>$v)
	{
		
	$ori_file = $v['file_name'];
	$ori_ext = $v['extension'];	
	$ori_url = $v['url'];
	$ori_ver =$v['version'];
	$userUploaded =  $uploaddir.$_FILES['userfile']['name'];
	$new = pathinfo($userUploaded);
	print_r($new);
	if ($ori_file!=$new['filename'].".".$new['extension'])
		{
			echo $ori_file."<--->".$new['filename'].".".$new['extension']."<br />";
			echo "文件更新失败,文件名/类型跟原文件不一致!<br /> 单击<a href ='".$url."'>这里</a>返回首页。";
		}
	else {
	// STEPS2: 把该文件上一版本删除->保存新版本->更新数据库
			if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploaddir.$ori_url))
			 {
				 echo "文件更新成功!。。。";
				 //数据库更新 : 
				 $update['version'] = $ori_ver + 1;
				 $db->update($id,$update,"tb_files");
				 
			} 
			else
			 {
				echo "文件更新失败!<br /> 单击<a href ='".$url."'>这里</a>返回首页。";
			}

		}
echo '调试信息:';
print_r($_FILES);

	}// end of foreach
}
echo "</pre>";

?> 
</body>
</html>
